package cloud.simple.zuul;

import javax.servlet.FilterRegistration;

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.FilterRegistrationBean;
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import com.netflix.zuul.FilterFileManager;
import com.netflix.zuul.FilterLoader;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.ContextLifecycleFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.filters.FilterRegistry;
import com.netflix.zuul.groovy.GroovyCompiler;
import com.netflix.zuul.groovy.GroovyFileFilter;
import com.netflix.zuul.http.ZuulServlet;
import com.netflix.zuul.monitoring.MonitoringHelper;


@SpringBootApplication
public class Application {

	public static void main(String[] args) {
		new SpringApplicationBuilder(Application.class).web(true).run(args);
	}
	
	@Component
	public static class MyCommandLineRunner implements CommandLineRunner{
		@Override
		public void run(String... args) throws Exception {
			MonitoringHelper.initMocks();
			initJavaFilters();
			FilterLoader.getInstance().setCompiler(new GroovyCompiler());
			try {
				FilterFileManager.setFilenameFilter(new GroovyFileFilter());
				FilterFileManager.init(10, "D:/javaTool/eclipse_4.6.0/workspace/cloud-simple-zuul/src/main/java/cloud/simple/zuul/filter/groovy/pre");
			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}
		
		private void initJavaFilters() {
            final FilterRegistry r = FilterRegistry.instance();
			
			r.put("javaPreFilter", new ZuulFilter(){

				@Override
				public Object run() {
					System.out.println("running javaPreFilter");
					RequestContext.getCurrentContext().set("name","hello filter pre!");
					return null;
				}

				@Override
				public boolean shouldFilter() {
					return true;
				}

				@Override
				public int filterOrder() {
					return 50000;
				}

				@Override
				public String filterType() {
					return "pre";
				}
				
			});
			
			r.put("javaRoutingFilter", new ZuulFilter(){

				@Override
				public Object run() {
					System.out.println("running javaRoutingFilter");
					try {
						RequestContext.getCurrentContext().getResponse().sendRedirect("http://www.baidu.com");
					} catch (Exception e) {
						e.printStackTrace();
					}
					return null;
				}

				@Override
				public boolean shouldFilter() {
					return true;
				}

				@Override
				public int filterOrder() {
					return 50000;
				}

				@Override
				public String filterType() {
					return "route";
				}
				
			});
			
			r.put("javaPostFilter", new ZuulFilter(){

				@Override
				public Object run() {
					System.out.println("running javaPostFilter");
					System.out.println(RequestContext.getCurrentContext().get("name"));
					return null;
				}

				@Override
				public boolean shouldFilter() {
					return true;
				}

				@Override
				public int filterOrder() {
					return 50000;
				}

				@Override
				public String filterType() {
					return "post";
				}
				
			});
		}
		
	}
	
	@Bean
	public ServletRegistrationBean zuulServlet(){
		ServletRegistrationBean servlet = new ServletRegistrationBean(new ZuulServlet());
		servlet.addUrlMappings("/test");
		return servlet;
	}
	
	@Bean
	public FilterRegistrationBean contextLifecycleFilter(){
		FilterRegistrationBean filter = new FilterRegistrationBean(new ContextLifecycleFilter());
		filter.addUrlPatterns("/*");
		return filter;
	}
}
